home *** CD-ROM | disk | FTP | other *** search
/ Alde ADA 1: #1 / CCCC 8804 Volume 1 Number 1 - Alde.iso / C / MISC / FUNC / PROFF.ARC / PROFF02.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-21  |  3.1 KB  |  180 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "defs.h"
  4. #include "debug.h"
  5.  
  6. /*
  7.  * addset - put c in array[i], if it fits, increment i
  8.  *
  9.  */
  10. int addset(c, array, i, maxsize)
  11. char c;
  12. char array[];
  13. int *i;
  14. int maxsize;
  15. {
  16.    int n, status = NO;
  17.  
  18.    dprintf("addset  ");
  19.    n = *i;
  20.    if (n <= maxsize - 1)
  21.    {
  22.       array[n++] = c;
  23.       status = YES;
  24.    }
  25.    *i = n;
  26.    return (status);
  27. }
  28.  
  29. /*
  30.  * addstr - add string s to str[i] if it fits, increment i
  31.  *
  32.  */
  33. int addstr(s, str, i, maxsize)
  34. char s[];
  35. char str[];
  36. int *i;
  37. int maxsize;
  38. {
  39.    int k, j, status = NO;
  40.  
  41.    dprintf("addstr  ");
  42.    j = *i;
  43.    if (j + strlen(s) <= maxsize - 1)
  44.    {
  45.       for (k = 0; s[k] != EOS; k++)
  46.          str[j++] = s[k];
  47.       status = YES;
  48.    }
  49.    *i = j;
  50.    return (status);
  51. }
  52.  
  53. /*
  54.  * ctoi - convert string at in[i] to integer, increment i
  55.  *
  56.  */
  57. int ctoi(in, i)
  58. char in[];
  59. int *i;
  60. {
  61.    int j, n, sign;
  62.  
  63.    dprintf("ctoi  ");
  64.    n = 0;
  65.    for (j = *i; in[j] == ' ' || in[j] == '\t'; j++)
  66.       ;                                /* skip leading garbage */
  67.    sign = 1;
  68.    if (in[j] == '+' || in[j] == '-')   /* sign */
  69.       sign = (in[j++] == '+') ? 1 : -1;
  70.    for (n = 0; in[j] >= '0' && in[j] <= '9'; j++)
  71.       n = 10 * n + in[j] - '0';
  72.    *i = j;
  73.    return (sign * n);
  74. }
  75.  
  76. /*
  77.  * error - print message and terminate
  78.  *
  79.  */
  80. error(s)
  81. char s[];
  82. {
  83.    fprintf(stderr, "%s\n", s);
  84.    exit(1);
  85. }
  86.  
  87. /*
  88.  * getwrd - get non-blank word from in[i] into out, increment i
  89.  *
  90.  */
  91. int getwrd(in, i, out)
  92. char in[];
  93. int *i;
  94. char out[];
  95. {
  96.    int j, size = 0;
  97.  
  98.    for (j = *i; in[j] == '\t' || in[j] == ' '; j++)
  99.       ;                                /* skip leading garbage */
  100.    while (in[j] != ' ' && in[j] != '\t' && in[j] != EOS && in[j] != '\n')
  101.       out[size++] = in[j++];
  102.    out[size++] = EOS;
  103.    *i = j;
  104. #ifdef DEBUG
  105.    printf("getwrd: %s\n", out);
  106. #endif
  107.    return (size);
  108. }
  109.  
  110. /*
  111.  * skipbl - skip blanks, tabs at str[i], increment i
  112.  *
  113.  */
  114. skipbl(str, i)
  115. char str[];
  116. int *i;
  117. {
  118.    int n;
  119.  
  120.    dprintf("skipbl  ");
  121.    n = *i;
  122.    while (str[n] == ' ' || str[n] == '\t')
  123.       n++;
  124.    *i = n;
  125. }
  126.  
  127. /*
  128.  * itoc - special version of itoa
  129.  *
  130.  */
  131. int itoc(n, str, size)
  132. int n;
  133. char str[];
  134. int size;
  135. {
  136.  
  137.    int i, j, k, sign;
  138.    char c;
  139.  
  140.    dprintf("itoc  ");
  141.    if ((sign = n) < 0)
  142.       n = -n;
  143.    i = 0;
  144.    do
  145.    {
  146.       str[i++] = n % 10 + '0';
  147.    } while ((n /= 10) > 0 && i < size - 2);
  148.    if (sign < 0 && i < size - 1)
  149.       str[i++] = '-';
  150.    str[i] = EOS;
  151.    /*
  152.     * reverse the string and plug it back in 
  153.     *
  154.     */
  155.    for (j = 0, k = strlen(str) - 1; j < k; j++, k--)
  156.    {
  157.       c = str[j];
  158.       str[j] = str[k];
  159.       str[k] = c;
  160.    }
  161.    return (i);
  162. }
  163.  
  164. /*
  165.  * usage - obvious..
  166.  *
  167.  */
  168. usage()
  169. {
  170. #ifdef rainbow
  171.    fprintf(stderr, "%s %s",
  172.            "usage: proff \033[7m[+n] [-n] [-v] [-ifile] [-s] [-pon]\033[0m",
  173.            "\033[1minfile\033[0m \033[7m[outfile]\033[0m\n");
  174. #else
  175.    fprintf(stderr,
  176.            "usage: proff [+n] [-n] [-v] [-ifile] [-s] [-pon] infile [outfile]");
  177. #endif
  178.    exit(0);
  179. }
  180.